﻿using System.Globalization;
using System.Text.RegularExpressions;

namespace StackOverflowClient
{
    public class Parser
    {
        /// <summary>
        ///  Regular expression built for C# Using Expresso Version: 3.0.2766, http://www.ultrapico.com
        ///  
        ///  A description of the regular expression:
        ///  
        ///  <img.*src="
        ///      <img
        ///      Any character, any number of repetitions
        ///      src="
        ///  [Image]: A named capture group. [.*PG]
        ///      .*PG
        ///          Any character, any number of repetitions
        ///          PG
        ///  " .*/>\W*</td>\W*</tr>\W*<tr>\W*<td\ class="summaryinfo">\W*<div\ class="summarycount">\W*
        ///      "
        ///      Space
        ///      Any character, any number of repetitions
        ///      />
        ///      Any character that is not alphanumeric, any number of repetitions
        ///      </td>
        ///      Any character that is not alphanumeric, any number of repetitions
        ///      </tr>
        ///      Any character that is not alphanumeric, any number of repetitions
        ///      <tr>
        ///      Any character that is not alphanumeric, any number of repetitions
        ///      <td
        ///      Space
        ///      class="summaryinfo">
        ///      Any character that is not alphanumeric, any number of repetitions
        ///      <div
        ///      Space
        ///      class="summarycount">
        ///      Any character that is not alphanumeric, any number of repetitions
        ///  [Reputation]: A named capture group. [[\d,\,]*]
        ///      Any character in this class: [\d,\,], any number of repetitions
        ///  
        ///
        /// </summary>
        public static Regex regexImageAndReputation = new Regex(
              "<img.*src=\"(?<Image>.*PG)\" .*/>\\W*</td>\\W*</tr>\\W*<tr>\\W" +
              "*<td\\ class=\"summaryinfo\">\\W*<div\\ class=\"summarycount" +
              "\">\\W*(?<Reputation>[\\d,\\,]*)",
            RegexOptions.CultureInvariant
            | RegexOptions.Compiled
            );

        /// <summary>
        ///  Regular expression built for C# Using Expresso Version: 3.0.2766, http://www.ultrapico.com
        ///  
        ///  A description of the regular expression:
        ///  
        ///  <table\ class="user-details">\W*<tr>\W*<td.*>\W*Name\W*</td>\W*<td.*>\W*<b>
        ///      <table
        ///      Space
        ///      class="user-details">
        ///      Any character that is not alphanumeric, any number of repetitions
        ///      <tr>
        ///      Any character that is not alphanumeric, any number of repetitions
        ///      <td
        ///      Any character, any number of repetitions
        ///      >
        ///      Any character that is not alphanumeric, any number of repetitions
        ///      Name
        ///      Any character that is not alphanumeric, any number of repetitions
        ///      </td>
        ///      Any character that is not alphanumeric, any number of repetitions
        ///      <td
        ///      Any character, any number of repetitions
        ///      >
        ///      Any character that is not alphanumeric, any number of repetitions
        ///      <b>
        ///  [Name]: A named capture group. [.*]
        ///      Any character, any number of repetitions
        ///  </b>\W*</td>\W*</tr>\W*<tr>
        ///      </b>
        ///      Any character that is not alphanumeric, any number of repetitions
        ///      </td>
        ///      Any character that is not alphanumeric, any number of repetitions
        ///      </tr>
        ///      Any character that is not alphanumeric, any number of repetitions
        ///      <tr>
        ///  
        ///
        /// </summary>
        public static Regex regexUserName = new Regex(
              "<table\\ class=\"user-details\">\\W*<tr>\\W*<td.*>\\W*Name\\W" +
              "*</td>\\W*<td.*>\\W*<b>(?<Name>.*)</b>\\W*</td>\\W*</tr>\\W*" +
              "<tr>",
            RegexOptions.CultureInvariant
            | RegexOptions.Compiled
            );

        /// <summary>
///  Regular expression built for C# Using Expresso Version: 3.0.2766, http://www.ultrapico.com
///  
///  A description of the regular expression:
///  
///  <div
///      <div
///  Any character that is NOT in this class: [>], one or more repetitions
///  class="summarycount"
///      class="summarycount"
///  Any character that is NOT in this class: [>], any number of repetitions
///  >
///  Any character that is NOT in this class: [\d], one or more repetitions
///  [Badges]: A named capture group. [\d+]
///      Any digit, one or more repetitions
///  </div>
///      </div>
///  Any character that is NOT in this class: [<], any number of repetitions
///  </td>
///      </td>
///  Any character that is NOT in this class: [<], any number of repetitions
///  <td
///      <td
///  Any character that is NOT in this class: [>], any number of repetitions
///  >
///  Any character that is NOT in this class: [<], any number of repetitions
///  <h1
///      <h1
///  Any character that is NOT in this class: [>], any number of repetitions
///  >Badges
///      >Badges
///  
///
/// </summary>
public static Regex regexNumberOgBadges = new Regex(
      "<div[^>]+class=\"summarycount\"[^>]*>[^\\d]+(?<Badges>\\d+)<"+
      "/div>[^<]*</td>[^<]*<td[^>]*>[^<]*<h1[^>]*>Badges",
    RegexOptions.IgnoreCase
    | RegexOptions.CultureInvariant
    | RegexOptions.IgnorePatternWhitespace
    | RegexOptions.Compiled
    );
        public static UserDescriptor Parse(string s)
        {
            var user = new UserDescriptor();

            var match1 = regexImageAndReputation.Match(s);
            user.Image = match1.Groups["Image"].Value;
            user.Reputation = int.Parse(match1.Groups["Reputation"].Value, NumberStyles.AllowThousands);

            var match2 = regexUserName.Match(s);
            user.Name = match2.Groups["Name"].Value;

            var match3 = regexNumberOgBadges.Match(s);
            user.NumberOfBadges = int.Parse( match3.Groups["Badges"].Value);
            
            //ParseUserQuestions(s);

            return user;
        }

        private static void ParseUserQuestions(string s)
        {
            int index = s.IndexOf("\"question-summary narrow\"");
            while(index > 0)
            {
            	
            	
            	index = s.IndexOf("\"question-summary narrow\"", index + 1);
            }
        }
    }
}
